package com.lzx.payment.controller.web;

import com.lzx.common.utils.R;
import com.lzx.payment.service.AliPayService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 程序员星星
 * @date 2022/12/14
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/payment/aliPay/web")
public class WebAliPayController {
    @Resource
    private AliPayService aliPayService;

    //统一收单下单并支付页面接口的调用
    @PostMapping("/trade/{activityId}/{userId}")
    public R tradePagePay(@PathVariable Long activityId, @PathVariable Long userId) {
        log.info("统一收单下单并支付页面接口的调用");
        return aliPayService.tradePagePay(activityId, userId);
    }

    //支付通知
    @PostMapping("/trade/notify")
    public R tradeNotify(@RequestParam Map<String, String> params) {
        log.info("支付通知正在执行");
        log.info("通知参数 ===> {}", params);
        return aliPayService.tradeNotify(params);
    }

    //用户取消订单
    @PostMapping("/trade/close/{orderNo}")
    public R cancel(@PathVariable String orderNo) {
        log.info("取消订单");
        return aliPayService.cancelOrder(orderNo);
    }

    //查询订单
    //如果未扫码，则订单不存在；扫码，则可查询到订单
    @GetMapping("/trade/query/{orderNo}")
    public R queryOrder(@PathVariable String orderNo) {
        log.info("查询订单");
        String result = aliPayService.queryOrder(orderNo);
        return R.ok().message("查询成功").data("result", result);
    }

    //申请退款
    @ApiOperation("申请退款")
    @PostMapping("/trade/refund/{orderNo}/{reason}")
    public R refunds(@PathVariable String orderNo, @PathVariable String reason) {
        log.info("申请退款");
        return aliPayService.refund(orderNo, reason);
    }

    //查询退款
    @GetMapping("/trade/query/refund/{orderNo}")
    public R queryRefund(@PathVariable String orderNo) {
        log.info("查询退款");
        String result = aliPayService.queryRefund(orderNo);
        return R.ok().message("查询成功").data("result", result);
    }
}
